<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\facade\Cache;
class Recycle extends Controller {
	
	public function index(){
		return $this->fetch();
	}
	
	public function recycle_lists(){
		$data = request()->param();
		$start = ($data['page']-1)*$data['limit'];
		$count = db("recycle")->count();
		$cacheId = to_guid_string(arr_to_html($data));
		$rest = db("recycle")
				->alias("a")
				->leftJoin("models b","a.model_id = b.id")
				->leftJoin("category c","a.cid = c.id")
				->field('a.*,b.table_name,c.name as category_name')
				->limit($start,$data['limit'])
				->cache($cacheId,3600,'recycle')
				->select();
		return showTable(0, 'OK', $count ,$rest, 200);
	}
	
	public function reduction(){
		$data = request()->param();
		// p($data);die();
		$r = db($data['table_name'])->where(['aid'=>$data['aid']])->update(['status'=>1]);
		if($r){
			$c = db('recycle')->where(['id'=>$data['id']])->delete();
			if($c){
				Cache::clear('recycle');
				return show(0, '还原成功', "", 200);
			}
			
		}
		return show(1, '还原失败', "", 200);
	}
	
	public function data_delete(){
		$data = request()->param();
		$c = db($data['table_name'])->where(['aid'=>$data['aid']])->delete();
		if($c){
			db('recycle')->where(['id'=>$data['id']])->delete();
			Cache::clear('recycle');
			return show(0, '删除成功', "", 200);
		}
		return show(1, '删除失败', "", 200);
	}
	
}